<template>
	<view class="container">
		<view class="tui-page__hd">
			<view class="tui-page__title">设置columns</view>
			<view class="tui-page__desc">
				<text>columns：</text>
				<slider value="3" :min="3" :max="5" :block-size="20" activeColor="#07c160" block-color="#07c160" show-value @change="sliderChange"></slider>
			</view>
		</view>
		<view class="tui-page__bd tui-page__spacing">
			<tui-drag ref="drag" @change="change" @sortend="sortEnd" :listData="list" :scrollTop="scrollTop" :itemHeight="itemHeight"
			 :columns="columns">
				<template slot-scope="{entity,height}">
					<view class="tui-image__item" :style="{height:height+'rpx'}">
						<image :src="entity.src" :style="{height:height+'rpx'}" mode="widthFix"></image>
					</view>
				</template>
			</tui-drag>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				itemHeight: 210,
				scrollTop: 0,
				list: [],
				columns: 3
			};
		},
		onLoad() {
			//如果异步返回数据，可手动调用初始化
			//this.$refs.drag && this.$refs.drag.init();
			let list = []
			for (let i = 0; i < 18; i++) {
				let img = i % 2 == 0 ? 1 : 2;
				list.push({
					id: i + 1,
					src: `/static/images/layout/${img}.jpg`
				})
			}
			this.list = list;
		},
		methods: {
			sliderChange(e) {
				uni.pageScrollTo({
					scrollTop: 0
				})
				this.columns = e.detail.value;
				this.itemHeight = [210, 160, 120][this.columns - 3]
			},
			sortEnd(e) {
				console.log('sortEnd', e.listData);
			},
			change(e) {
				console.log('change', e.listData);
			}
		}
	};
</script>

<style>
	.tui-page__desc {
		width: 100%;
		display: flex;
		align-items: center;
	}

	.tui-page__desc slider {
		flex: 1;
	}

	.tui-image__item {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: #fff;
		padding: 20rpx;
		box-sizing: border-box;
		border-top: 1rpx solid #eee;
		border-left: 1rpx solid #eee;
	}

	.tui-image__item image {
		flex-shrink: 0;
		width: 100%;
		display: block;
	}
</style>
